from sqlalchemy.orm import Session
from app.db.db_models import Number
from typing import Optional

class DBNumber:
    def __init__(self, session: Session):
        self.session = session

    def get_latest_number(self) -> Optional[Number]:
        """获取最新的数字"""
        return self.session.query(Number).order_by(Number.id.desc()).first()

    def add_number(self, value: int, user_id: int) -> Number:
        """添加新的数字"""
        new_number = Number(value=value, user_id=user_id)
        self.session.add(new_number)
        self.session.commit()
        self.session.refresh(new_number)
        return new_number
